define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shopro/lucky/user/index' + location.search,
                    add_url: 'shopro/lucky/user/add',
                    edit_url: 'shopro/lucky/user/edit',
                    del_url: 'shopro/lucky/user/del',
                    rollback_url: 'shopro/lucky/user/rollback',
                    multi_url: 'shopro/lucky/user/multi',
                    multicheck_url: 'shopro/lucky/user/multi_check',
                    import_url: 'shopro/lucky/user/import',
                    table: 'shopro_lucky_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                rowStyle: function (row, index) {
                    if (row.userinfo.order_switch === 1) {
                        return {
                            css: {"color" : 'red !important'} // Bootstrap 浅色成功类
                        };
                    } else {
                        return {};
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'server.name', title: __('Server.name'), operate: 'LIKE'},
                        {field: 'userinfo.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'userinfo.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'userinfo.money', title: '剩余消费券', operate: 'BETWEEN'},
                        {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        {field: 'score', title: __('Score'), operate:'BETWEEN'},
                        {field: 'number', title: __('Number')},
                        {field: 'p_number', title: __('P_number')},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'p_total_price', title: __('P_total_price'), operate:'BETWEEN'},
                        {field: 'proportion', title: __('Proportion')},
                        {field: 'is_lucky', title: __('Is_lucky'), searchList: {"0":__('Is_lucky 0'),"1":__('Is_lucky 1'),"2":__('Is_lucky 2')}, formatter: Table.api.formatter.normal},

                        {field: 'stutas', title: __('Stutas'), searchList: {"0":__('Stutas 0'),"1":__('Stutas 1'), "-1": "已撤回"}, formatter: Table.api.formatter.normal},
                        {field: 'server.open_lucky_time', title: __('Server.open_lucky_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},



                        //{field: 'server.createtime', title: __('Server.createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[{
                                name:'info',
                                text:'查看',
                                icon:'fa fa-list',
                                classname:'btn btn-info btn-xs btn-detail btn-dialog',
                                url:'shopro/lucky/order?lucky_user_id={row.id}'
                            },{
                                name: 'pass',
                                text: '撤回',
                                title: '确认撤回订单',
                                classname: 'btn btn-success btn-ajax btn-xs',
                                icon: 'fa fa-check',
                                confirm: '确定要撤回吗?',
                                url:'shopro/lucky/user/rollback?lucky_user_id={row.id}',
                                success: function (data, ret) {
                                    table.bootstrapTable('refresh');
                                },
                                error: function (data, ret) {
                                    console.log(data, ret);
                                }
                            }]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
